from rest_framework import serializers
from note_dir.models import NoteDir
from user.models import User
from user.serializers import UserSerializer


class NoteDirSerializer(serializers.ModelSerializer):
    """笔记目录序列化器"""

    user = serializers.SerializerMethodField(read_only=True)

    class Meta:
        model = NoteDir
        exclude = ['user_id', 'is_delete']

    def get_user(self, obj):
        user = User.objects.filter(id=obj.user_id).first()
        if user:
            return UserSerializer(user).data
        else:
            return None
